package com.kidback.bbloc259.util;

import android.app.ActivityManager;
import android.content.Context;

import androidx.annotation.NonNull;

import com.kidback.bbloc259.app.KidApplication;

import java.util.List;

/**
 * Created by meikai on 2020/06/02.
 */
public class ProcessUtils {

    public static boolean isMainProcess() {
        return StringUtils.equals(getProcessName(), KidApplication.getInstance().getPackageName());
    }

    /**
     * 进程判断
     * AndroidProcesses.getRunningAppProcesses() 方法非常耗时
     * 因此用 ActivityManager 的 getRunningAppProcesses
     */
    @NonNull
    public static String getProcessName() {
        int myPid = android.os.Process.myPid();
        ActivityManager am = (ActivityManager) KidApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            List<ActivityManager.RunningAppProcessInfo> list = am.getRunningAppProcesses();
            if (list != null) {
                for (ActivityManager.RunningAppProcessInfo info : list) {
                    if (info.pid == myPid) {
                        if (info.processName != null) {
                            return info.processName;
                        }
                    }
                }
            }
        }
        return "";
    }


}
